﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Web.Security;

namespace Loja
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Cliente"] != null) {
                Response.Redirect(Page.ResolveUrl(Request.Params["ReturnUrl"].ToString()));
            }
            String script = @" $(document).ready(function () {
                                    $('.btnEntrar').button({ icons: { primary: 'ui-icon-key'} }); //.css('margin', '25px 0 20px 0px');

                                    $( '#dialogErro' ).dialog({
			                                autoOpen: false,
			                                modal: true,
			                                buttons: {
				                                OK: function() {
                                                    document.getElementById('lblErro').innerText = """";                                                    
                                                    //debugger;
					                                $( this ).dialog( 'close' );
                                                    
				                                },
			                                },
			                        });
                                    
                                    if(document.getElementById('lblErro').innerText != """")
                                        var abre = $('#dialogErro').dialog('open');

                                });";

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "javas", script, true);
        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
           /*if (!String.IsNullOrEmpty(Request.Params["ReturnUrl"]))
            {
                //Response.Redirect(Page.ResolveUrl(Request.Params["ReturnUrl"]));
                Response.Redirect(Page.ResolveUrl(Page.ResolveUrl("~/../loja/customer/") + "editcustomer.aspx?redirect=" + Page.ResolveUrl("~../../store/home.aspx")));
            }
            else { */
            Response.Redirect(Page.ResolveUrl("~/../loja/customer/") + "editcustomer.aspx?redirect=dashboard"); //}
        }

        protected void btnEntrar_Click(object sender, EventArgs e)
        {

            using(LojaChocolateDCDataContext dc = new LojaChocolateDCDataContext())
            {
                Table<Cliente> tblCliente = dc.GetTable<Cliente>();
                string _Email = txtEmail.Text;
                Donini.Crypt.Crypt cript = new Donini.Crypt.Crypt();
                cript.Key = "chocolate";
                string _Senha = cript.Encrypt(txtSenha.Text);
                Cliente cliente = tblCliente.Where( c => c.CliMail.Trim() == _Email && c.CliSenha.Trim() == _Senha).SingleOrDefault();
                if (cliente != null)
                {
                    FormsAuthenticationTicket fat = new FormsAuthenticationTicket("cliente",false,30);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat));
                    Response.Cookies.Add(cookie);

                    Session.Add("Cliente",cliente);
                    FormsAuthentication.RedirectFromLoginPage(cliente.CliID.ToString(), true);                    
                }
                else {
                    lblErro.Text = "Erro ao acessar, verifique usuário ou senha. <br /> Para cadastrar um usuário novo <a href='~/../customer/editcustomer.aspx' >clique aqui</a>.";
                }
                
            }
        }

        protected void btnEsqueceu_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/customer/RecoveryPassword.aspx"));
        }
    }
}